package com.hrm.controller;

import com.hrm.model.HrmApartment;
import com.hrm.service.HrmApartmentService;
import com.hrm.util.PageView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/Apart")
public class HrmApartController {
    @Autowired
    protected HrmApartmentService hrmApartmentService;

    /**
     * 部门页面
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/hrmApartManage.html")
    public ModelAndView hrmApartManage(
            @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize) {
        Map<String, Object> params = new HashMap<String, Object>();
        PageView pageView = new PageView(pageNum);
        pageView.setPageSize(pageSize);

        ModelAndView mav = new ModelAndView("/hrmapartment");
        try {
            mav.addObject("pageView", hrmApartmentService.findEntityPageByParam(pageView, params));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mav;
    }

    /**
     * 根据id获取部门信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/hrmApartId.post", method = RequestMethod.POST)
    @ResponseBody
    public ModelAndView hrmApartIdManage(Integer id) {
        ModelAndView mav = new ModelAndView("/page/apartInfo_update");
        try {
            mav.addObject("hrmApart", hrmApartmentService.getHrmApart(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mav;
    }

    /**
     * 更新部门信息
     * @param hrmApartment
     * @return
     */
    @RequestMapping(value = "/updateApartInfo.post",method = RequestMethod.POST)
    @ResponseBody
    public Integer hrmApartInfoUpdateManage(HrmApartment hrmApartment) {
        try {
            hrmApartmentService.updateApartInfo(hrmApartment);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @RequestMapping(value ="/deleteByApartId.post",method = RequestMethod.POST)
    @ResponseBody
    public Integer deleteByApartId(Integer id){
        try
        {
            hrmApartmentService.deleteApartById(id);
            return 1;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return 0;
        }
    }
}
